package com.expressage.controller;

import com.expressage.po.CityAddress;
import com.expressage.service.CityAddressService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.ArrayList;
import java.util.List;

/**
 * 市控制器
 */
@Controller
@RequestMapping("/jspafter")
public class CityAddressController {

    @Autowired
    CityAddressService cityAddressService;
    Logger logger=Logger.getLogger(CityAddressController.class);

    @RequestMapping("/findallcity")
    public String findallcity(Model model){
        List<CityAddress> list=new ArrayList<>();
        list =cityAddressService.findAll();
        for (CityAddress c:list) {
            logger.info(c.getCityid()+"\t"+c.getCityname());
        }
        model.addAttribute("list",list);
        return "jspafter/citylist";
    }
    @RequestMapping("/toaddcity")
    public String toaddCity(){

        return "jspafter/addcity";
    }
    @RequestMapping("/addcity")
    public String addCity(CityAddress cityAddress){
        boolean flag=cityAddressService.insert(cityAddress);
        if(flag){
            return "redirect:findallcity.action";
        }else {
            return "";
        }

    }
    @RequestMapping("/findByCityid")
    public String findByCityid(int id,Model model){
        CityAddress cityAddress =cityAddressService.findByCityid(id);
        model.addAttribute("cityAddress",cityAddress);
        return "";
    }
    @RequestMapping("/deletecity")
    public String deleteCity(int[] id){
        int id1=id[0];
        CityAddress cityAddress=new CityAddress();
        cityAddress.setCityid(id1);
        cityAddress.setCityname("删除状态");
        boolean flag=cityAddressService.update(cityAddress);

        if(flag){
            return "redirect:findallcity.action";
        }else {
           return "";
        }
    }

    //城市更新
    @RequestMapping("/touptecity")
    public String toUpdateCity(int id,Model model){
        CityAddress cityAddress =cityAddressService.findByCityid(id);
        model.addAttribute("cityAddress",cityAddress);
        return "jspafter/updatecity";
    }
    @RequestMapping("updatecity")
    public String updateCity(CityAddress cityAddress){
        boolean flag=cityAddressService.update(cityAddress);
        if(flag){
            return "redirect:findallcity.action";
        }else {
            return "";
        }
    }
}
